package com.bicon.botu.mall.order.processor.task.schedule;

import com.bicon.botu.mall.order.cache.RedisLock;
import com.bicon.botu.mall.order.processor.task.service.AfterSalesTaskService;
import com.cxytiandi.elasticjob.annotation.ElasticJobConf;
import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

@Slf4j
//@ElasticJobConf(name = "AfterSalesRefundJob")
public class AfterSalesJob implements SimpleJob{

    private static final String lock = "after_sales_refund_task_lock";

    @Autowired
    private RedisLock redisLock;

    @Autowired
    private AfterSalesTaskService afterSalesTaskService;

    @Override
    public void execute(ShardingContext shardingContext) {
        // 获取锁
        long lockTimeOut = redisLock.lock(lock,1000);
        if (lockTimeOut>0){
            log.info("开始执行退款任务");
            afterSalesTaskService.refund();
            // 释放锁
            redisLock.releaseLock(lock,lockTimeOut);
            log.info("结束执行退款任务");
        }else{
            log.info("售后自动退款定时任务未获取到任务锁 不再执行");
        }

    }
}
